package com.nondo.rdp.finance.common;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class OrderUtil {
	
	private static Logger _log = LoggerFactory.getLogger(OrderUtil.class);
	
	private static AtomicLong seq = new AtomicLong(0L);
	
	/**
	 * 生成商户订单号
	 * @param uid 
	 * @return
	 */
	public static String getMchOrderNo(Long uid){
		long timestamp = System.currentTimeMillis();
		String mchOrderNo = String.format("%s%s%d%03d", "G", timestamp, uid, seq.getAndIncrement());
		return mchOrderNo;
	}
	
	/**
	 * 通知参数信息转换
	 */
	public static Map<String, Object> request2payResponseMap(HttpServletRequest request, String[] paramArray) {
        Map<String, Object> responseMap = new HashMap<>();
        for (int i = 0;i < paramArray.length; i++) {
            String key = paramArray[i];
            String v = request.getParameter(key);
            if (v != null) {
                responseMap.put(key, v);
            }
        }
        return responseMap;
    }
	
	/**
	 * 验证通知信息
	 * @param map
	 * @return
	 */
	public static boolean verifyPayResponse(Map<String,Object> map) {
        String mchId = (String) map.get("mchId");
        String payOrderId = (String) map.get("payOrderId");
        String mchOrderNo = (String) map.get("mchOrderNo");
        String amount = (String) map.get("amount");
        String sign = (String) map.get("sign");

        if (StringUtils.isEmpty(mchId)) {
            _log.warn("Params error. mchId={}", mchId);
            return false;
        }
        if (StringUtils.isEmpty(payOrderId)) {
            _log.warn("Params error. payOrderId={}", payOrderId);
            return false;
        }
        if (StringUtils.isEmpty(mchOrderNo)) {
            _log.warn("Params error. mchOrderNo={}", mchOrderNo);
            return false;
        }
        if (StringUtils.isEmpty(amount) || !NumberUtils.isNumber(amount)) {
            _log.warn("Params error. amount={}", amount);
            return false;
        }
        if (StringUtils.isEmpty(sign)) {
            _log.warn("Params error. sign={}", sign);
            return false;
        }

        String resKey = null;
        
        // 验证签名
        if (!verifySign(map, mchId, resKey)) {
            _log.warn("verify params sign failed. payOrderId={}", payOrderId);
            return false;
        }

        return true;
    }
	
	/**
	 * 验证签名信息
	 * @param map
	 * @return
	 */
	public static boolean verifySign(Map<String, Object> map, String omchId, String resKey) {
        String mchId = (String) map.get("mchId");
        if(!omchId.equals(mchId)) return false;
        String localSign = PayDigestUtil.getSign(map, resKey, "sign");
        String sign = (String) map.get("sign");
        return localSign.equalsIgnoreCase(sign);
    }
	
	public static void main(String[] args) {
		System.out.println(getMchOrderNo(1L));
	}
}
